<?PHP

class Admin_order_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','田园卡订单');
		$this->assign('menuid1','4'); 
	}
	
	
	##################################
	# 公用
	##################################
	/* 几个订单统计数量 */
	public function loadorder_num__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$db = factory::getdbo();
			# 全部订单
			$order_all = $db->loadresult('select count(*) from t_order  ');   
			# 系统取消
			$order_cancel2 = $db->loadresult('select count(*) from t_order where status=-2   ');   
			# 用户取消
			$order_cancel1 = $db->loadresult('select count(*) from t_order where status=-1    ');   
			# 待审核
			$order_1 = $db->loadresult('select count(*) from t_order where status=1    ');   
			# 已审核，待生产
			$order_2 = $db->loadresult('select count(*) from t_order where status=2    ');   
			# 已生产，待配送
			$order_3 = $db->loadresult('select count(*) from t_order where status=3    ');   
			# 已配送
			$order_4 = $db->loadresult('select count(*) from t_order where status=4    ');   
			
			exit('{success:1,order_all:"'.$order_all.'",order_cancel2:"'.$order_cancel2.'",order_cancel1:"'.$order_cancel1.'",order_1:"'.$order_1.'",order_2:"'.$order_2.'",order_3:"'.$order_3.'",order_4:"'.$order_4.'"}');
		}
		exit();
	}
	
	
  /* 订单列表 */
	public function list__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';
			if(!isset($_POST['types']))   $_POST['types']  = '';
			if(!isset($_POST['status']))    $_POST['status']  = ''; 
			if(!isset($_POST['b_date']))    $_POST['b_date']  = ''; 
			if(!isset($_POST['e_date']))    $_POST['e_date']  = ''; 
			if(!isset($_POST['rec_bdate']))    $_POST['rec_bdate']  = '';
			if(!isset($_POST['rec_edate']))    $_POST['rec_edate']  = '';
			if(!isset($_POST['sendtime']))    $_POST['sendtime']  = '';
  	  #查询条件
			$sql_where = ' where 1=1 '.func::speedsearch($_POST['keywords'],array( 'a.code' 	,'b.name','b.username','a.price_total' ));
			if($_POST['sendtime'] != ''){
				$sql_where .= ' and a.sendtime="'.$_POST['sendtime'].'" ';
			}
			if($_POST['status'] != ''){
				$sql_where .= ' and a.status="'.$_POST['status'].'" ';
			}
			if($_POST['b_date']!=''){
				$sql_where .= ' and a.created >= "'.$_POST['b_date'].' 00:00:00"';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and a.created <= "'.$_POST['e_date'].' 23:59:59"';	
			}
			if($_POST['rec_bdate']!=''){
				$sql_where .= ' and a.rec_date >= "'.$_POST['rec_bdate'].'"';	
			}
			if($_POST['rec_edate']!=''){
				$sql_where .= ' and a.rec_date <= "'.$_POST['rec_edate'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_order` a
			left join t_user b on a.user_id=b.id
			left join t_list_sendtime c on a.sendtime=c.id
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					b.name as user_name,
					b.username,
					c.val as sendtime
				from `t_order` a
				left join t_user b on a.user_id=b.id
			left join t_list_sendtime c on a.sendtime=c.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
		exit();	
	}

	/* 订单查看 */
	public function view__(){
		
		$this->assign('nav','订单详情');  
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.*,b.username,b.name as user_name,c.val as sendtime  from `t_order` a left join t_user b on a.user_id=b.id left join t_list_sendtime c on a.sendtime=c.id  where a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1'); 
		 
		$rs['order_goods'] = $db->loadassoclist('select * from t_order_goods where order_id="'.$rs['id'].'" ');
		foreach($rs['order_goods'] as &$val ){
			$val['order_goods_item'] = $db->loadassoclist('select a.*,b.name as vege_name from t_order_goods_item a left join t_vege b on a.vege_id=b.id where a.order_id="'.$rs['id'].'" and a.order_goods_id="'.$val['id'].'" ');
		}
	 	$this->assign('rs', $rs ); 
		
	}




	
  /* 日志列表 */
	public function loglist__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'ASC';
			if(!isset($_POST['order_id']))				$_POST['order_id']				= '0';
  	  #查询条件
			$sql_where = ' where order_id="'.$_POST['order_id'].'" ';
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_order_log` a
			left join t_user b on a.created_user_id=b.id
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					b.name as user_name,
					b.username
			from `t_order_log` a
			left join t_user b on a.created_user_id=b.id
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
		exit();	
	}


	##################################
	# 修改配送日期
	##################################
	public function recdate_modify__(){
		//权限判断
		factory::module('admin')->authSys('order_ok');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST); 
			$db = factory::getDBO(); 
			$rs = $db->loadassoc('select * from t_order where id="'.$_POST['id'].'"');
			if($rs['status'] != '1'){
				exit('操作失败！只允许修改【审核中】的订单！');
			}
			$db->begin(); 
			//主表
			if(!$db->update('order',array(
				'id'=>$_POST['id'],
				'rec_date'=>$_POST['rec_date'],
				'sendtime'=>$_POST['sendtime']
			))){
				exit('操作失败！');
			}
			$db->commit();
			exit('{success:1,msg:"修改成功！"}');
		}
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_order` a where a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1');
		$this->assign('rs',$rs);	
		if($rs['status']!='1'){
			exit('只能修改待审核的订单');
		}
		# 配送时间
		$this->assign('sendtime',$db->loadassoclist('select id,val from t_list_sendtime where (deleted=0 or id="'.$rs['sendtime'].'")'));
	}
  
  
	
	

	##################################
	# 审核
	##################################
	public function index_sh__(){
		//权限判断
		factory::module('admin')->authSys('order_ok');
	 	$this->assign('nav','订单审核');
		$this->assign('menuid2','2'); 
	}
	public function sh_complete__(){
		//权限判断
		factory::module('admin')->authSys('order_ok');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$ids = format::addslash_json(json_decode( stripslashes($_POST['ids']) )); 
		 
			$db = factory::getDBO();
			$db->begin();
			
			if(is_array($ids)){
				foreach($ids as $v){
					$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$v.'" ');
					if(!$rs){
						continue;
					}
					if($rs['status']!='1'){
						continue;
					}
					//主表
					if(!$db->update('order',array(
						'id'=>$v,
						'status'=>'2'
					))){
						exit('操作失败，请刷新页面继续操作！');
					}
					# 日志 $order_id,$logs,$user_id
					factory::module('order')->logs($v,'审核通过',factory::module('admin')->getuserid());
				}	
			}
			
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	public function sh_uncomplete__(){
		//权限判断
		factory::module('admin')->authSys('order_ok');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$ids = format::addslash_json(json_decode( stripslashes($_POST['ids']) )); 
		
			$db = factory::getDBO();
			$db->begin();
			 
			if(is_array($ids)){
				foreach($ids as $v){
					$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$v.'" ');
					if(!$rs){
						continue;
					}
					if($rs['status']!='2'){
						continue;
					}
					//主表
					if(!$db->update('order',array(
						'id'=>$v,
						'status'=>'1'
					))){
						exit('操作失败！');
					}
					# 日志 $order_id,$logs,$user_id
					factory::module('order')->logs($v,'撤销审核',factory::module('admin')->getuserid());
				}
			}
			
			
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}


	

	##################################
	# 生产
	##################################
	public function index_make__(){
		//权限判断
		factory::module('admin')->authSys('order_make');
	 	$this->assign('nav','订单生产');
		$this->assign('menuid2','3'); 
	}
	public function make_complete__(){
		//权限判断
		factory::module('admin')->authSys('order_make');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$ids = format::addslash_json(json_decode( stripslashes($_POST['ids']) )); 
		
			$db = factory::getDBO();
			$db->begin();
			 
			if(is_array($ids)){
				foreach($ids as $v){
					$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$v.'" ');
					if(!$rs){
						continue;
					}
					if($rs['status']!='2'){
						continue;
					}
					//主表
					if(!$db->update('order',array(
						'id'=>$v,
						'status'=>'3'
					))){
						exit('操作失败！');
					}
					# 日志 $order_id,$logs,$user_id
					factory::module('order')->logs($v,'已生产',factory::module('admin')->getuserid());
				}
			}		
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	public function make_uncomplete__(){
		//权限判断
		factory::module('admin')->authSys('order_make');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$ids = format::addslash_json(json_decode( stripslashes($_POST['ids']) )); 
		
			$db = factory::getDBO();
			$db->begin();
			 
			if(is_array($ids)){
				foreach($ids as $v){
					 
					$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$v.'" ');
					if(!$rs){
						continue;
					}
					if($rs['status']!='3'){
						continue;
					}
					//主表
					if(!$db->update('order',array(
						'id'=>$v,
						'status'=>'2'
					))){
						exit('操作失败！');
					}
					# 日志 $order_id,$logs,$user_id
					factory::module('order')->logs($v,'撤销已生产',factory::module('admin')->getuserid());
				}
			}		
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	

 
	

	##################################
	# 配送
	##################################
	public function index_send__(){
		//权限判断
		factory::module('admin')->authSys('order_send');
	 	$this->assign('nav','订单配送');
		$this->assign('menuid2','4'); 
	}
	public function send_complete__(){
		//权限判断
		factory::module('admin')->authSys('order_send');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$ids = format::addslash_json(json_decode( stripslashes($_POST['ids']) )); 
		
			$db = factory::getDBO();
			$db->begin();
			 
			if(is_array($ids)){
				foreach($ids as $v){
					 
					$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$v.'" ');
					if(!$rs){
						continue;
					}
					if($rs['status']!='3'){
						continue;
					}
					//主表
					if(!$db->update('order',array(
						'id'=>$v,
						'status'=>'4'
					))){
						exit('操作失败！');
					}
					# 日志 $order_id,$logs,$user_id
					factory::module('order')->logs($v,'已配送',factory::module('admin')->getuserid());
				}
			}		
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	public function send_uncomplete__(){
		//权限判断
		factory::module('admin')->authSys('order_send');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$ids = format::addslash_json(json_decode( stripslashes($_POST['ids']) )); 
		
			$db = factory::getDBO();
			$db->begin();
			 
			if(is_array($ids)){
				foreach($ids as $v){
					$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$v.'" ');
					if(!$rs){
						continue;
					}
					if($rs['status']!='4'){
						continue;
					}
					//主表
					if(!$db->update('order',array(
						'id'=>$v,
						'status'=>'3'
					))){
						exit('操作失败！');
					}
					# 日志 $order_id,$logs,$user_id
					factory::module('order')->logs($v,'撤销已配送',factory::module('admin')->getuserid());
				}
			}		
			$db->commit();
			exit('{success:1}');
		}
		exit();
	}
	

 
	

	##################################
	# 订单取消
	##################################
	public function index_cancel__(){
		//权限判断
		factory::module('admin')->authSys('order_cancel');
	 	$this->assign('nav','订单取消');
		$this->assign('menuid2','5'); 
	}
	public function cancel_complete__(){
		//权限判断
		factory::module('admin')->authSys('order_cancel');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$db = factory::getDBO();
			$db->begin();
			 
			$rs = $db->loadassoc('select a.* from `t_order` a where  a.id="'.$_POST['id'].'" ');
			if(!$rs){
				exit('没有查到任何记录！');
			}
			if($rs['status']=='-1' || $rs['status']=='-2' ){
				exit('操作失败，订单状态已经是取消状态！');
			}
			
			#得到传递过来的参数 
			$m_order  = factory::module('order');
			if(!$m_order->cancelBySys($rs['id'],$rs['user_id'])){
				exit('操作失败 '.$m_order->getError());
			}
			
			$db->commit();
			exit('{success:1}');
		}
		exit();
	} 

 
 
 

	##################################
	# 修改，删除原有的，新增
	##################################
	public function modify__(){
		//权限判断
		factory::module('admin')->authSys('order_ok');
		
		
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$db = factory::getdbo();
			$m_order  = factory::module('order');
			$db->begin();
			$_POST=format::addslash_deep($_POST);
			$combos = format::addslash_json(json_decode( stripslashes($_POST['combos']) ));
			$order_id = $_POST['order_id'];
			$rec_date = $_POST['rec_date'];
			$sendtime = $_POST['sendtime'];
			
		
			$rs = $db->loadassoc('select a.*,b.username,b.name as user_name from `t_order` a left join t_user b on a.user_id=b.id  where a.id="'.$order_id.'" ');
			if(!$rs){
				exit('没有查到原有订单信息！'); 
			}
			if($rs['status'] != '1'){
				exit('操作失败！只允许修改【审核中】的订单！');
			}
			
			# 取消原有订单
			if(!$m_order->cancelBySys($rs['id'],$rs['user_id'])){
				exit('操作失败 '.$m_order->getError());
			}
			
			
			# 新下订单
			$user_id = $rs['user_id'];
			$ret = $m_order->submitOrder($user_id,$combos,factory::module('admin')->getuserid(),true,$rec_date,$sendtime);
			if(!$ret){
				exit('操作失败：'.$m_order->getError()); 
			}
			$db->commit();
			exit('{success:1,order_id:"'.$ret['id'].'",ordercode:"'.$ret['code'].'",price_all:"'.$ret['price_all'].'",price_deliver:"'.$ret['price_deliver'].'",price_total:"'.$ret['price_total'].'"}');
		}
 		
		
				
		$db = factory::getdbo();
		# 菜品信息
		$this->assign('rs_veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
			
		$this->assign('nav','修改订单');  
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		
		$rs = $db->loadassoc('select a.*,b.username,b.name as user_name from `t_order` a left join t_user b on a.user_id=b.id  where a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1'); 
		
		if($rs['status'] != '1'){
			url::msgbox('操作失败！只允许修改【审核中】的订单！','close');
		}
	 
		
		$rs['order_goods'] = $db->loadassoclist('select * from t_order_goods where order_id="'.$rs['id'].'" ');
		foreach($rs['order_goods'] as &$val ){
			$val['order_goods_item'] = $db->loadassoclist('select a.*,b.name as vege_name from t_order_goods_item a left join t_vege b on a.vege_id=b.id where a.order_id="'.$rs['id'].'" and a.order_goods_id="'.$val['id'].'" ');
		}
	 	$this->assign('rs', $rs ); 
		
		# 配送时间
		$this->assign('sendtime',$db->loadassoclist('select id,val from t_list_sendtime where (deleted=0 or id="'.$rs['sendtime'].'")'));
	}
	

	##################################
	# 新增
	##################################
	public function add__(){
		//权限判断
		factory::module('admin')->authSys('order_add');
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			$combos = format::addslash_json(json_decode( stripslashes($_POST['combos']) ));
			$user_id = $_POST['user_id'];
			$rec_date = $_POST['rec_date'];
			$sendtime = $_POST['sendtime'];
			$m_order = factory::module('order');
			$ret = $m_order->submitOrder($user_id,$combos,factory::module('admin')->getuserid(),true,$rec_date,$sendtime);
			if(!$ret){
				exit('操作失败：'.$m_order->getError()); 
			}
			exit('{success:1,order_id:"'.$ret['id'].'",ordercode:"'.$ret['code'].'",price_all:"'.$ret['price_all'].'",price_deliver:"'.$ret['price_deliver'].'",price_total:"'.$ret['price_total'].'"}');
		}	
	 	$this->assign('nav','新增订单');
		$this->assign('menuid2','6'); 
		$db = factory::getdbo();
		# 菜品信息
		$this->assign('rs_veges',$db->loadassoclist('select * from `t_vege` where deleted=0 ','id'));
		# 配送时间
		
		$this->assign('sendtime',factory::module('list')->getlist('sendtime'));
	}
 
 	public function add_load_combo__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST);
			# 回调函数 funcname 
			$id = $_POST['id'];
			$db = factory::getdbo();
			$nowdate = date('Y-m-d H:i:s');
			$rs = $db->LoadAssoc('
			select
				a.*
			from `t_combo` a  
			 where a.id="'.$id.'" and a.deleted=0  
			');
			if(!$rs){
				exit('没有找到产品信息');
			}
			$rs['combo_item'] = $db->loadassoclist('select a.*,b.name as vege_name,b.price from t_combo_item a left join t_vege b on a.vege_id=b.id where a.combo_id="'.$rs['id'].'" ');
			exit('{"success":"1","rs":'.json_encode($rs).'}');
		}
		exit();
	}
 
  public function add_loadsenddate__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$m_order = factory::module('order');
			$comboids = format::addslash_json(json_decode( stripslashes($_POST['combo_ids']) ));
			//debug::show($comboids);
			$combosendarea = $m_order->combosSendArea($comboids);
			if($combosendarea === false){
				exit('您当前选择的菜品，可配送时间段不一致，请先选择部分菜品下单！');
			}
			$rec_date = $m_order->combosSendArea($comboids,true);
			exit('{"success":"1","rec_date":'.json_encode($rec_date).'}');
		}
		exit();
	}
 
 
 
 
 
}
?>